package com.cvit.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cvit.model.Examinationtable;
import com.cvit.model.Msg;
import com.cvit.service.ExaminationtableService;
import com.cvit.service.StudentService;
import com.cvit.utils.PDFUpload;

@Controller
public class PDFTicketController {

	@Autowired
	ExaminationtableService examinationtableService;
	@Autowired
	StudentService studentService;

	@RequestMapping("/ProducePDF")
	public @ResponseBody Msg exportA(HttpServletRequest request, HttpServletResponse response, String eamineeId)
			throws Exception {
		List<Examinationtable> student = examinationtableService.getByEamineeId(eamineeId);
		PDFUpload pdf = new PDFUpload();
		Examinationtable examinationtable1 = new Examinationtable();
		if (student.size() != 0) {
			if (student.size() == 2) {
				Examinationtable examinationtable2 = new Examinationtable();
				examinationtable1 = student.get(0);
				examinationtable2 = student.get(1);
				
				String date1 = examinationtable1.getTestTime();
				String date2 = examinationtable2.getTestTime();
				
				if (date1.length() > date2.length()) {
					pdf.produce(request, response, examinationtable1, examinationtable2);
					System.out.println("1");
					// 修改打印状态值
					studentService.updateTicketByEamineeId(examinationtable1.getEamineeId());
				} else{ 
					System.out.println("2");
					pdf.produce(request, response, examinationtable2, examinationtable1);
					studentService.updateTicketByEamineeId(examinationtable1.getEamineeId());
				}
			} else {
				Examinationtable examinationtable3 = new Examinationtable();
				examinationtable1 = student.get(0);
				pdf.produce(request, response, examinationtable1, examinationtable3);
				studentService.updateTicketByEamineeId(examinationtable1.getEamineeId());
			}
		}else{
			return Msg.fail().add("code", "考生号输入有误");
			}
		return null;
	}

	
	
}
